package com.bw.usercenter.ui

import android.annotation.SuppressLint
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import com.bw.common.sp.SPPropDelegate
import com.bw.mvpcore.view.BaseMVPActivity
import com.bw.net.protocol.resp.BaseRespEntity
import com.bw.usercenter.R
import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.req.UserEntitiy
import com.bw.usercenter.model.protocol.resp.*
import com.bw.usercenter.presenter.UserCenterPresenterImpl
import com.bw.usercenter.sq.MySQ
import com.bw.usercenter.sq.MySQHelper
import kotlinx.android.synthetic.main.activity_log.*
import kotlinx.android.synthetic.main.activity_reg.*

class RegActivity : BaseMVPActivity<UserCenterPresenterImpl>(),UserCenterContract.UserCenterView{

    private var username : String by SPPropDelegate<String>("username","testuser")
    private var userpass : String by SPPropDelegate<String>("userpass","testuser")

    var db: SQLiteDatabase?=null
    /**
     * 获取视图
     */
    override fun getLayoutId(): Int {
        return  R.layout.activity_reg
    }
    /**
     * 初始化
     */
    @SuppressLint("NewApi")
    override fun initEvent() {
        var mySQ: MySQ = MySQ(this@RegActivity)
        db = mySQ.writableDatabase as SQLiteDatabase
        logbtnok.setOnClickListener {
            var name = log_username.text.toString()
            var pass = log_userpass.text.toString()
            var sex = log_sex.text.toString()
            username = name
            userpass = pass
            mPresenter.register(UserEntitiy("2021-01-01",0,pass,sex,name))
        }
    }
    /**
     * 初始化数据
     */
    override fun initData(savedInstanceState: Bundle?){

    }
    override fun createPresenter(): UserCenterPresenterImpl {
        return UserCenterPresenterImpl(this)
    }

    override fun ClearStatBar() {

    }
    override fun releaseResource() {

    }

    override fun <T> registerSuccess(data: T) {
        showToast("注册成功,即将返回登录")
        val a : BaseRespEntity<RespUserEntity> = data as BaseRespEntity<RespUserEntity>
        val cv = ContentValues()
        cv.put("name",a.data.username)
        cv.put("pass",a.data.pwd)
        cv.put("sex",a.data.sex)
        db?.insert("user",null,cv)
        MyQueryUser()
        jumpActivity(LogActivity::class.java)
    }
    fun MyQueryUser(){
        var userlise:ArrayList<UserBean> = arrayListOf()
        val cursor= db!!.query("user",null,null,null,null,null,null)
        if (cursor.moveToFirst()){
            while (cursor.moveToNext()){
                val user = UserBean()
                user.name = cursor.getString(cursor.getColumnIndex("name"))
                user.pass = cursor.getString(cursor.getColumnIndex("pass"))
                user.sex = cursor.getString(cursor.getColumnIndex("sex"))
                println("--账号:${user.name}--密码:${user.pass}--性别:${user.sex}")
                userlise.add(user)
            }
        }

    }

    override fun registerSuccess(data: RespRecommendEntity) {
         
    }

    override fun registerFaild(throwable: Throwable) {
        showToast("注册失败")
    }

    override fun registerSrech(respSearchEntity: RespSearchEntity) {

    }

    override fun getSearchitem(respSearchitemIDEntity: RespSearchitemIDEntity) {

    }
}